package databasetest;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class Input extends JInternalFrame {

	private JTextField code;
	private JTextField ea;
	private JButton btnSave;
	private JLabel lblCode;
	private JLabel lblEa;

	Connection con;
	DatabaseTest dt;
	
	public Input(Connection c,DatabaseTest o) {
		super("input", true, true, true, false);
		this.con = c;
		this.dt = o;
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(null);
		getContentPane().add(getCode());
		getContentPane().add(getEa());
		getContentPane().add(getBtnSave());
		getContentPane().add(getLblCode());
		getContentPane().add(getLblEa());

	}

	private JTextField getCode() {
		if (code == null) {
			code = new JTextField();
			code.setBounds(97, 65, 116, 21);
			code.setColumns(10);
		}
		return code;
	}

	private JTextField getEa() {
		if (ea == null) {
			ea = new JTextField();
			ea.setColumns(10);
			ea.setBounds(97, 96, 116, 21);
		}
		return ea;
	}

	private JButton getBtnSave() {
		if (btnSave == null) {
			btnSave = new JButton("Save");
			btnSave.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {

					try {
						String c = code.getText();
						int ev = Integer.parseInt(ea.getText());
						String sql = "insert into PTbl values(?,?,ptbl_serial.nextval)";
						PreparedStatement pstmt = con.prepareStatement(sql);
						pstmt.setString(1, c);
						pstmt.setInt(2, ev);
						pstmt.executeUpdate();
						
					if (dt.search !=null)
						dt.search.search();
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}

				}
			});
			btnSave.setBounds(97, 127, 97, 23);
		}
		return btnSave;
	}

	private JLabel getLblCode() {
		if (lblCode == null) {
			lblCode = new JLabel("code");
			lblCode.setBounds(28, 68, 57, 15);
		}
		return lblCode;
	}

	private JLabel getLblEa() {
		if (lblEa == null) {
			lblEa = new JLabel("ea");
			lblEa.setBounds(28, 99, 57, 15);
		}
		return lblEa;
	}
}
